﻿using CPPEI.Coolzon.Data;
using CPPEI.Coolzon.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.DataProvider.Cache
{
    public static class DataResultCache
    {
        private static DataSourceIndex s_sourceIndex = new DataSourceIndex();

        public static DataResult GetData(string userIdentity,
            CPPEI.Coolzon.Metadata.DataSource dataSource, 
            CPPEI.Coolzon.Metadata.DataSet dataSet, 
            List<QueryParameter> parameters)
        {
            // 综合数据源和数据集的缓存模式，得到最终的缓存模式
            CacheMode cacheMode = dataSet.CacheMode;
            if (cacheMode == CacheMode.None)
            {
                cacheMode = dataSource.CacheMode;
            }

            if (cacheMode == CacheMode.Share)
            {
                DataSourceCache dataSourceNode = s_sourceIndex.Get(dataSource.Name);
                if (dataSourceNode == null)
                {
                    return null;
                }
                DataSetCache dataSetNode = dataSourceNode.Get(dataSet.Name);
                if (dataSetNode == null)
                {
                    return null;
                }
                string paramString = GetParameterString(parameters);
                DataParameterCache dataParameterNode = dataSetNode.Get(paramString);
                if (dataParameterNode == null)
                {
                    return null;
                }

                //Common.Logger.DebugToTag("Cache", "get result datasource:{0} dataset:{1} param:{2}",
                //    dataSource.Name,
                //    dataSet.Name,
                //    paramString);

                return dataParameterNode.DataResult;
            }
            return null;
        }

        public static void StoreData(string userIdentity,
            CPPEI.Coolzon.Metadata.DataSource dataSource,
            CPPEI.Coolzon.Metadata.DataSet dataSet,
            List<QueryParameter> parameters,
            DataResult dataResult)
        {
            // 综合数据源和数据集的缓存模式，得到最终的缓存模式
            CacheMode cacheMode = dataSet.CacheMode;
            if (cacheMode == CacheMode.None)
            {
                cacheMode = dataSource.CacheMode;
            }

            if (cacheMode == CacheMode.Share)
            {
                DataSourceCache dataSourceNode = s_sourceIndex.Store(dataSource.Name);
                DataSetCache dataSetNode = dataSourceNode.Store(dataSet.Name);
                string paramString = GetParameterString(parameters);
                dataSetNode.Store(paramString, dataResult);
            }
        }

        private static string GetParameterString(List<QueryParameter> parameters)
        {
            StringBuilder stringBuilder = new StringBuilder();
            for (int index = 0; index < parameters.Count; index++)
            {
                stringBuilder.AppendFormat(",{0}={1},", parameters[index].Name, parameters[index].ValueString);
            }
            return stringBuilder.ToString();
        }
    }
}
